package base;

import java.sql.Timestamp;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashSet;
import java.util.Set;

import org.json.*;

import pojo.Difficulty;

public class JsonUnpacker {
	public static final int NULLINT = -2;
	public static final long NULLLONG = -2;
	public static final double NULLDOUBLE = -2;

	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	
	private JsonHelper jh;
	
	public JsonUnpacker(){
		jh = new JsonHelper();
	}
	
	public JSONObject getJsonObject(String str){
		try {
			return new JSONObject(str);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private String initString(String key,JSONObject jo){
		try {
			return jo.getString(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	private int initInt(String key,JSONObject jo){
		try {
			return jo.getInt(key);
		} catch (JSONException e) {
			return NULLINT;
		}
	}
	
	private long initLong(String key,JSONObject jo){
		try {
			return jo.getLong(key);
		} catch (JSONException e) {
			return NULLLONG;
		}
	}
	
	private double initDouble(String key,JSONObject jo){
		try {
			return jo.getDouble(key);
		} catch (JSONException e) {
			return NULLDOUBLE;
		}
	}
	
	private Object initObject(String key,JSONObject jo){
		try {
			return jo.get(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	private JSONObject initJSONObject(String key,JSONObject jo){
		try {
			return jo.getJSONObject(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	private JSONArray initArray(String key,JSONObject jo){
		try {
			return jo.getJSONArray(key);
		} catch (JSONException e) {
			return null;
		}
	}
	
	private Timestamp getTimestamp(String str){
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		java.util.Date date11;
		try {
			date11 = df1.parse(str);
			String time = df1.format(date11);
			Timestamp ts = Timestamp.valueOf(time);
			return ts;
		} catch (ParseException e) {
			return null;
		}
	}

}
